﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using WPFChat.Model;
using WPFChat.ViewModel;

namespace WPFChat.Controller
{
    public class NetworkController
    {
        private IClient _client;

        public NetworkController()
        {
            _client = new Client();
            _client.MessageReceivedEvent += _client_MessageReceivedEvent;
        }

        public void SendMessage(Message message)
        {
            _client.Send(message);
        }

        private void _client_MessageReceivedEvent(Message message)
        {
            Debug.WriteLine("[INFO] MessageReceivedEvent Fired");

            App.ApplicationController.MainWindow.Dispatcher.Invoke(DispatcherPriority.Normal, (Action) delegate()
            {
                App.ApplicationController.ApplicationViewModel.ChatViewModel.Messages.Add(new MessageViewModel()
                {
                    Sender = message.Sender,
                    State = message.State,
                    TimeStamp = message.TimeStamp,
                    Content = message.Content,
                    ContainedObject = message
                });
            });
        }
    }
}
